package com.example.ycdyus.dailyfruit.fragment;


import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.ycdyus.dailyfruit.R;
import com.example.ycdyus.dailyfruit.adapter.GuoshiViewPagerAdapter;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
@EFragment(R.layout.fragment_guoshi)
public class GuoshiFragment extends Fragment {
    @ViewById
    RadioButton rbGuoshiBaikeHeadbar;
    @ViewById
    RadioButton rbGuoshiQuanHeadbar;
    @ViewById
    RadioGroup rgGuoshiBaikeHeadbar;
    @ViewById
    ViewPager vpGuoshiBaike;


    private List<Fragment> list = new ArrayList<>();
    private GuoshiViewPagerAdapter adapter;


    //初始化View之后
    @AfterViews
    public void init(){
        //设置默认值
        ((RadioButton)rgGuoshiBaikeHeadbar.getChildAt(0)).setChecked(true);
        ((RadioButton)rgGuoshiBaikeHeadbar.getChildAt(0)).setBackgroundColor(getResources().getColor(R.color.white));
        ((RadioButton)rgGuoshiBaikeHeadbar.getChildAt(1)).setBackgroundColor(Color.rgb(113,184,68));
        rgGuoshiBaikeHeadbar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_guoshi_baike_headbar:
                        vpGuoshiBaike.setCurrentItem(0);
                        setRadioButtonBackGround(0, rgGuoshiBaikeHeadbar.getChildCount());
                        break;
                    case R.id.rb_guoshi_quan_headbar:
                        vpGuoshiBaike.setCurrentItem(1);
                        setRadioButtonBackGround(1, rgGuoshiBaikeHeadbar.getChildCount());
                        break;
                }
            }
        });
        /**
         * 处理ViewPager
         */
        list.add(new BaikeFragment_());
        list.add(new GuoyouquanFragment_());
        adapter = new GuoshiViewPagerAdapter(getChildFragmentManager(),list);
        vpGuoshiBaike.setAdapter(adapter);
        vpGuoshiBaike.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setRadioButtonBackGround(position,rgGuoshiBaikeHeadbar.getChildCount());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setRadioButtonBackGround(int position,int sum){
        for (int i = 0; i < sum; i++) {
            ((RadioButton)rgGuoshiBaikeHeadbar.getChildAt(i)).setChecked(false);
            ((RadioButton)rgGuoshiBaikeHeadbar.getChildAt(i)).setBackgroundColor(Color.rgb(113,184,68));
        }

        ((RadioButton)rgGuoshiBaikeHeadbar.getChildAt(position)).setBackgroundColor(getResources().getColor(R.color.white));
        ((RadioButton)rgGuoshiBaikeHeadbar.getChildAt(position)).setChecked(true);
    }
}
